package org.dandsoft.blackbox.snn;

import org.dandsoft.blackbox.ObjectId;
import org.dandsoft.blackbox.Value;
import org.dandsoft.blackbox.ChannelException;
import org.dandsoft.blackbox.util.list.Entry;

/**
 * User: dvilyuzhanin
 * Date: 19.01.2009 18:19:08
 */
public class Synapse {

    private Enviroment enviroment;
    private ObjectId id;
    private ObjectId remoteId;
    private ObjectId source;
    private ObjectId destination;
    private SynapseType type;
    private Value value;
    private Value weight;
    private Value minValue = Value.MIN_VALUE;
    private Value maxValue = Value.MAX_VALUE;
    private Value newValue;
    private final Entry<Synapse> usedEntry = new Entry<Synapse>(this);
    private final Entry<Synapse> activeEntry = new Entry<Synapse>(this);

    protected synchronized void used() throws SnnException {
        enviroment.addToUsedSynapsesList(usedEntry);
    }


    public synchronized void activate() throws SnnException {
        this.value = newValue;
        if (value.isInRage(minValue, maxValue)) {
            if (SynapseType.Output != type) {
                Neuron neuron = enviroment.getNeuron(destination);
                neuron.addActiveSynapseEntry(activeEntry);
            } else {
                enviroment.output(remoteId, value);

            }
        }
    }

    public synchronized void prepare(Value value) throws SnnException {
        setNewValue(value);
        used();
    }

    public synchronized Value getValue() {
        return value;
    }

    public synchronized Value getNewValue() {
        return newValue;
    }

    public synchronized void setNewValue(Value newValue) {
        this.newValue = newValue;
    }

    public synchronized SynapseType getType() {
        return type;
    }

    public synchronized void setType(SynapseType type) {
        this.type = type;
    }


    public synchronized ObjectId getId() {
        return id;
    }

    public synchronized void setId(ObjectId id) {
        this.id = id;
    }

    public synchronized ObjectId getSource() {
        return source;
    }

    public synchronized void setSource(ObjectId source) {
        this.source = source;
    }

    public synchronized ObjectId getDestination() {
        return destination;
    }

    public synchronized void setDestination(ObjectId destination) {
        this.destination = destination;
    }

    public synchronized ObjectId getRemoteId() {
        return remoteId;
    }

    public synchronized void setRemoteId(ObjectId remoteId) {
        this.remoteId = remoteId;
    }

    public synchronized Enviroment getEnviroment() {
        return enviroment;
    }

    public synchronized void setEnviroment(Enviroment enviroment) {
        this.enviroment = enviroment;
    }

    public synchronized Value getWeight() {
        return weight;
    }

    public synchronized void setWeight(Value weight) {
        this.weight = weight;
    }
}
